package cn.zh.service.impl;

import cn.zh.commons.ResponseResult;
import cn.zh.entity.Slideshow;
import cn.zh.mapper.CarouselMapper;
import cn.zh.service.CarouselService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class CarouselServiceImpl implements CarouselService {
    @Resource
    private CarouselMapper carouselMapper;

    public ResponseResult list() {
        List<Slideshow> slideshows =carouselMapper.list();
        return ResponseResult.success().data(slideshows);
    }


    public ResponseResult slidePage(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);

        List<Slideshow> slideshows=carouselMapper.slidePage();
        PageInfo<Slideshow> slideshowPageInfo = new PageInfo<Slideshow>(slideshows);
        long total = slideshowPageInfo.getTotal();
        return ResponseResult.success().data(slideshows).count(total);
    }


    public ResponseResult editslide(Integer id) {

        carouselMapper.editslide(id);
        return ResponseResult.success().message("置顶成功");
    }


    public ResponseResult deleteSlide(Integer id, String img) {
        //根据id删除数据库的信息
        carouselMapper.deleteSlideById(id);

        //TODO
        //根据uuid删除本地磁盘中的信息
        File file = new File("D:/IDEAFrame/upload/slideShow/" + img);
        file.delete();


        return ResponseResult.success().message("删除成功");
    }


    public ResponseResult uploadInfo(String title, String uuid) {
        carouselMapper.uploadInfo(title,uuid);

        return ResponseResult.success().message("添加成功");
    }
}
